<template>
  <div class="resource-usage-chart">
    <div class="resource-grid">
      <div class="resource-item" v-for="(item, index) in resourceItems" :key="index">
        <div class="resource-header">
          <div class="resource-icon" :style="{ background: item.gradient }">
            <el-icon><component :is="item.icon" /></el-icon>
          </div>
          <div class="resource-info">
            <div class="resource-name">{{ item.name }}</div>
            <div class="resource-value">{{ item.value }}%</div>
          </div>
        </div>
        <div class="resource-progress">
          <el-progress 
            :percentage="item.value" 
            :color="item.color"
            :stroke-width="8"
            :show-text="false"
            :status="item.status" />
          <div class="progress-labels">
            <span class="progress-label">0%</span>
            <span class="progress-label">{{ item.value }}%</span>
            <span class="progress-label">100%</span>
          </div>
        </div>
        <div class="resource-details">
          <div class="detail-item">
            <span class="detail-label">已使用:</span>
            <span class="detail-value">{{ item.used }}</span>
          </div>
          <div class="detail-item">
            <span class="detail-label">总容量:</span>
            <span class="detail-value">{{ item.total }}</span>
          </div>
          <div class="detail-item">
            <span class="detail-label">状态:</span>
            <span class="detail-status" :class="item.statusClass">{{ item.statusText }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Cpu, Monitor, Histogram, Connection } from '@element-plus/icons-vue';

export default {
  name: 'ResourceUsageChart',
  components: {
    Cpu, Monitor, Histogram, Connection
  },
  props: {
    resourceData: {
      type: Object,
      required: true
    }
  },
  computed: {
    resourceItems() {
      return [
        {
          name: 'CPU使用率',
          value: this.resourceData.cpu,
          icon: 'Cpu',
          gradient: 'linear-gradient(135deg, #fa709a 0%, #fee140 100%)',
          color: '#fa709a',
          used: `${this.resourceData.cpu}%`,
          total: '100%',
          status: this.getStatus(this.resourceData.cpu),
          statusText: this.getStatusText(this.resourceData.cpu),
          statusClass: this.getStatusClass(this.resourceData.cpu)
        },
        {
          name: '内存使用率',
          value: this.resourceData.memory,
          icon: 'Monitor',
          gradient: 'linear-gradient(135deg, #a8edea 0%, #fed6e3 100%)',
          color: '#a8edea',
          used: `${this.resourceData.memory}%`,
          total: '100%',
          status: this.getStatus(this.resourceData.memory),
          statusText: this.getStatusText(this.resourceData.memory),
          statusClass: this.getStatusClass(this.resourceData.memory)
        },
        {
          name: '磁盘使用率',
          value: this.resourceData.disk,
          icon: 'Histogram',
          gradient: 'linear-gradient(135deg, #43e97b 0%, #38f9d7 100%)',
          color: '#43e97b',
          used: `${this.resourceData.disk}%`,
          total: '100%',
          status: this.getStatus(this.resourceData.disk),
          statusText: this.getStatusText(this.resourceData.disk),
          statusClass: this.getStatusClass(this.resourceData.disk)
        },
        {
          name: '网络使用率',
          value: this.resourceData.network,
          icon: 'Connection',
          gradient: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
          color: '#667eea',
          used: `${this.resourceData.network}%`,
          total: '100%',
          status: this.getStatus(this.resourceData.network),
          statusText: this.getStatusText(this.resourceData.network),
          statusClass: this.getStatusClass(this.resourceData.network)
        }
      ];
    }
  },
  methods: {
    getStatus(value) {
      if (value >= 90) return 'exception';
      if (value >= 80) return 'warning';
      if (value >= 60) return 'success';
      return 'success';
    },
    
    getStatusText(value) {
      if (value >= 90) return '危险';
      if (value >= 80) return '警告';
      if (value >= 60) return '正常';
      return '优秀';
    },
    
    getStatusClass(value) {
      if (value >= 90) return 'status-danger';
      if (value >= 80) return 'status-warning';
      if (value >= 60) return 'status-normal';
      return 'status-excellent';
    }
  }
};
</script>

<style scoped>
.resource-usage-chart {
  width: 100%;
  height: 100%;
  padding: 16px;
}

.resource-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
  height: 100%;
}

.resource-item {
  background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%);
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  border: 1px solid #e2e8f0;
}

.resource-item:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.resource-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}

.resource-icon {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 20px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.resource-info {
  flex: 1;
}

.resource-name {
  font-size: 14px;
  font-weight: 600;
  color: #64748b;
  margin-bottom: 4px;
}

.resource-value {
  font-size: 24px;
  font-weight: 700;
  color: #1e293b;
  line-height: 1.2;
}

.resource-progress {
  margin-bottom: 16px;
}

.progress-labels {
  display: flex;
  justify-content: space-between;
  margin-top: 8px;
}

.progress-label {
  font-size: 11px;
  color: #94a3b8;
  font-weight: 500;
}

.resource-details {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.detail-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
}

.detail-label {
  color: #64748b;
  font-weight: 500;
}

.detail-value {
  color: #1e293b;
  font-weight: 600;
}

.detail-status {
  padding: 2px 8px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 600;
}

.status-excellent {
  background: #d1fae5;
  color: #059669;
}

.status-normal {
  background: #dbeafe;
  color: #2563eb;
}

.status-warning {
  background: #fef3c7;
  color: #d97706;
}

.status-danger {
  background: #fee2e2;
  color: #dc2626;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .resource-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
  
  .resource-item {
    padding: 16px;
  }
  
  .resource-icon {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }
  
  .resource-value {
    font-size: 20px;
  }
}

/* 自定义进度条样式 */
:deep(.el-progress-bar__outer) {
  background-color: #f1f5f9 !important;
  border-radius: 8px !important;
}

:deep(.el-progress-bar__inner) {
  border-radius: 8px !important;
  transition: all 0.3s ease;
}

:deep(.el-progress--line) {
  margin-bottom: 0 !important;
}
</style> 